﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Dynamic;
using System.IO;
using System.Net;
using System.Text;
using System.Web.Script.Serialization;

namespace myApp.parser
{
    class Program
    {
        static List<dynamic> база = new List<dynamic>();

        static void Main(string[] args)
        {
            var request = WebRequest.Create("http://iss.gov.spb.ru/phone/scripts/main/index.php?org=b");
            using (var response = (HttpWebResponse)request.GetResponse())
            using (var dataStream = response.GetResponseStream())
            using (var reader = new StreamReader(dataStream, Encoding.GetEncoding("windows-1251")))
            {
                var отрасли = Отрасли(reader.ReadToEnd());
                var json = GetJSON(отрасли);

                var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "json.txt");
                using (var fileWriter = new StreamWriter(path, false))
                {
                    fileWriter.Write(json);
                    fileWriter.Close();
                }
            }
        }

        private static List<dynamic> Отрасли(string html)
        {
            var lst = new List<dynamic>();

            var i = html.IndexOf("<div class=\"content\">", StringComparison.Ordinal);
            if (i > 0)
            {
                int iStart;
                int iEnd;
                var isTrue = true;

                while (isTrue)
                {
                    iStart = html.IndexOf("padding-left:40px; padding-bottom:5px", i);
                    if (iStart > 0)
                    {
                        iEnd = html.IndexOf("padding-left:40px; padding-bottom:5px", iStart + 1);
                        if (iEnd == -1)
                        {
                            iEnd = html.Length - 1;
                            isTrue = false;
                        }

                        var отрасль = Отрасль(html.Substring(iStart, iEnd - iStart));
                        lst.Add(отрасль);
                        i = iEnd;
                    }
                }
            }

            return lst;
        }

        private static dynamic Отрасль(string html)
        {
            var i = html.IndexOf("&nbsp;", StringComparison.Ordinal);
            if (i > 0)
            {
                var iStart = i + "&nbsp;".Length;
                var iEnd = html.IndexOf("</div>");
                var title = html.Substring(iStart, iEnd - iStart);
                title = title.Replace("\n", "").Replace("\t", "");

                dynamic отрасль = new ExpandoObject();
                база.Add(отрасль);

                отрасль.название = title;
                Организации(отрасль, html.Substring(iEnd));

                return отрасль;
            }

            return null;
        }

        private static void Организации(dynamic отрасль, string html)
        {
            отрасль.организации = new List<dynamic>();

            var isTrue = true;
            do
            {
                var iStart = html.IndexOf("<a href=\"", StringComparison.Ordinal);
                if (iStart > 0)
                {
                    var iEnd = html.IndexOf("</a>", StringComparison.Ordinal);
                    Организация(отрасль, html.Substring(iStart, iEnd - iStart));

                    html = html.Substring(iEnd + "</a>".Length);
                }
                else
                    isTrue = false;
            } while (isTrue);
        }

        private static void Организация(dynamic отрасль, string html)
        {
            var iStart = html.IndexOf("href=\"") + "href=\"".Length;
            var iEnd = html.LastIndexOf("\">");
            var url = html.Substring(iStart, iEnd - iStart);

            iStart = iEnd + "\">".Length;
            iEnd = html.Length - 1;
            var title = html.Substring(iStart, iEnd - iStart).Trim();

            dynamic организация = new ExpandoObject();
            организация.название = title;
            отрасль.организации.Add(организация);

            КонтактыОрганизации(организация, url);
        }

        private static void КонтактыОрганизации(dynamic организация, string url)
        {
            try
            {
                var request = WebRequest.Create(url);

                using (var response = (HttpWebResponse)request.GetResponse())
                using (var dataStream = response.GetResponseStream())
                using (var reader = new StreamReader(dataStream, Encoding.GetEncoding("windows-1251")))
                {
                    var html = reader.ReadToEnd();

                    //var iStart = <div class="YellowBack">Адрес:</div>

                    // ..
                    организация.адрес = GetAddress(html);
                    организация.телефоны = GetPhones(html);
//                    организация.факс = "";
//                    организация.почте = "";

                    Console.WriteLine("Org: {0}", url);
                }
            }
            catch (NotSupportedException)
            {
            }
        }

        private static string GetAddress(string html)
        {
            var i = html.IndexOf("<div class=\"divData\">", StringComparison.Ordinal);
            if (i > 0)
            {
                html = html.Substring(i + "<div class=\"divData\">".Length);
                var iStart = html.IndexOf("<b>");
                if (iStart >= 0)
                {
                    html = html.Substring(iStart + "<b>".Length);
                    var iEnd = html.IndexOf("</b>");
                    return html.Substring(0, iEnd);
                }
            }
            return "";
        }

        private static List<string> GetPhones(string html)
        {
            var lst = new List<string>();

            var iStart = html.IndexOf("<div class=\"YellowBack\">Средства связи:</div>") + "<div class=\"YellowBack\">Средства связи:</div>".Length;
            var iEnd = html.IndexOf("<div class=\"YellowBack\">Должностные лица</div>");
            if (iEnd == -1)
                iEnd = html.Length;

            html = html.Substring(iStart, iEnd - iStart).Replace("\n", "").Replace("&nbsp;", "").Replace(" ", "").Replace("\t", "").Replace(">т.", "");

            var str = "<divstyle=\"padding:2px2px2px2px\"><b><imgborder=\"0\"src=\"http://iss.gov.spb.ru/phone/img/tel.gif\"align=\"middle\"";

            var i = 0;
            var isTrue = true;
            do
            {
                iStart = html.IndexOf(str, i);
                if (iStart >= 0)
                {
                    iStart += str.Length;
                    iEnd = html.IndexOf("</b>", iStart);
                    lst.Add(html.Substring(iStart, iEnd - iStart));
                    i = iEnd;
                }
                else
                    isTrue = false;

            } while (isTrue);

            return lst;
        }

        private static string GetJSON(IEnumerable<dynamic> отрасли)
        {
            var ser = new JavaScriptSerializer();
            ser.RegisterConverters(new JavaScriptConverter[] { new ExpandoJsonConverter() });

            var sb = new StringBuilder();
            foreach (dynamic o in отрасли)
            {
                var json = ser.Serialize(o);
                sb.AppendLine(json);
            }
            return sb.ToString();
        }

        public class ExpandoJsonConverter : JavaScriptConverter
        {
            public override object Deserialize(IDictionary<string, object> dictionary, Type type, JavaScriptSerializer serializer)
            {
                // See source code link for this extension method at the bottom of this post (/Helpers/IDictionaryExtensions.cs)
                //return dictionary.ToExpando();

                throw new NotImplementedException();
            }
            public override IDictionary<string, object> Serialize(object obj, JavaScriptSerializer serializer)
            {
                var result = new Dictionary<string, object>();
                var dictionary = obj as IDictionary<string, object>;
                foreach (var item in dictionary)
                    result.Add(item.Key, item.Value);
                return result;
            }
            public override IEnumerable<Type> SupportedTypes
            {
                get
                {
                    return new ReadOnlyCollection<Type>(new Type[] { typeof(ExpandoObject) });
                }
            }
        }
    }
}
